package com.situ.meetingRoom.dao.impl;

import com.situ.meetingRoom.dao.BookingDao;
import com.situ.meetingRoom.entity.Booking;
import com.situ.meetingRoom.util.DBUtil;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.BookingVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class BookingDaoImpl implements BookingDao {
    private DBUtil dbUtil = new DBUtil();
    @Override
    public PageInfo selectForPage(Integer page, Integer limit, Booking booking) {
        String sqlWhere = "";
        List<Object> params = new ArrayList<>();

        if (booking.getBoardroomId() != null) {
            sqlWhere += " and boardroom_id = ? ";
            params.add(booking.getBoardroomId());
        }
        if (booking.getUserId() != null) {
            sqlWhere += " and user_id = ? ";
            params.add(booking.getUserId());
        }

        if (booking.getStatus() != null) {
            sqlWhere += " and booking.status = ? ";
            params.add(booking.getStatus());
        }

        //记录的条数
        String sql = " select count(*) count from booking where 1=1 " + sqlWhere;
        List<Map<String, Object>> list1 = dbUtil.query(sql, params.toArray());
        Long count = (Long) list1.get(0).get("count");


        sql = "select booking.*,boardroom.name boardroomName,user.name userName,user.tel userTel,user.email userEmail from booking inner join boardroom on boardroom.id=booking.boardroom_id inner join user on user.id=booking.user_id where 1=1  " + sqlWhere +
                " limit ?,? ";

        params.add((page - 1) * limit);
        params.add(limit);

        List list = dbUtil.query(sql, params.toArray());

        PageInfo pageInfo = new PageInfo(count, list);

        return pageInfo;
    }

    @Override
    public List<BookingVO> selectAll(Booking booking) {
        String sql = "select booking.*,boardroom.name boardroomName,user.name userName,user.tel userTel,user.email userEmail from booking inner join boardroom on boardroom.id=booking.boardroom_id inner join user on user.id=booking.user_id where 1=1 ";
        List<Object> params = new ArrayList<>();
        if (booking.getBoardroomId() != null) {
            sql += " and boardroom_id = ? ";
            params.add(booking.getBoardroomId());
        }
        if (booking.getUserId() != null) {
            sql += " and user_id = ? ";
            params.add(booking.getUserId());
        }

        if (booking.getStatus() != null) {
            sql += "and booking.status = ? ";
            params.add(booking.getStatus());
        }

        return dbUtil.query(BookingVO.class, sql, params.toArray());
    }

    @Override
    public Booking selectById(Integer id) {
        String sql = "select * from booking where id=?";
        return dbUtil.queryOne(Booking.class, sql, id);
    }

    @Override
    public int update(Booking booking) {
        String sql = "update booking " +
                "set ";
        List<Object> params = new ArrayList<>();

        if (booking.getBoardroomId() != null) {
            sql += "boardroom_id=?,";
            params.add(booking.getBoardroomId());
        }

        if (booking.getUserId() != null) {
            sql += "user_id=?,";
            params.add(booking.getUserId());
        }

        if (booking.getStartTime() != null) {
            sql += "start_time=?,";
            params.add(booking.getStartTime());
        }
        if (booking.getTime() != null && booking.getTime().length() > 0) {
            sql += "time=?,";
            params.add(booking.getTime());
        }

        if (booking.getStatus() != null) {
            sql += "status=?,";
            params.add(booking.getStatus());
        }
        sql = sql.substring(0, sql.length() - 1);
        sql += " where id = ?";
        params.add(booking.getId());
        return dbUtil.update(sql, params.toArray());
    }

    @Override
    public int insert(Booking booking) {
        String sql = "insert into booking (boardroom_id,user_id,start_time,time) values (?,?,?,?)";
        return dbUtil.update(sql, booking.getBoardroomId(), booking.getUserId(), booking.getStartTime(), booking.getTime());
    }

    @Override
    public Booking selectUserId(Integer id) {
        String sql = "select * from booking where user_id=?";
        return dbUtil.queryOne(Booking.class, sql, id);
    }

    @Override
    public Booking selectBoardroomId(Integer id) {
        String sql = "select * from booking where status=0 and boardroom_id=?";
        return dbUtil.queryOne(Booking.class, sql, id);
    }
}
